# 导入模块
from flask import Flask, render_template
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import json
import io
import seaborn as sns
import plotly.express as px
from wordcloud import WordCloud, STOPWORDS
import nltk
from nltk.corpus import stopwords
import imageio

app = Flask(__name__)

# 1.读数据
def read_data1():
    return pd.read_csv(r'C://Users/lenovo/Desktop/数据分析期末/data_out/tmdb_5000_movies.csv', encoding='utf-8', delimiter="\t",lineterminator="\n")
def read_data2():
    return pd.read_csv(r'C://Users/lenovo/Desktop/数据分析期末/data_out/tmdb_5000_credits.csv',encoding='utf-8',delimiter="\t",lineterminator="\n")

# 2.定义http的endpoint终结点/path
# 目标： 读取 不同电影类型的表格，并展示在网页中


@app.route('/get_movies')
def get_movies_info():
	movies=read_data1()
	credits=read_data2()
  # 调用读取数据的方法
  # 用json.loads把genres变成字典列表后，遍历每一个genres的长度，用name取值得到每一个列值对应的name列表
	
	movies["genres"]=movies["genres"].apply(json.loads)
	for index,i in zip(movies.index,movies['genres']):
	    list1=[]
	for j in range(len(i)):
	    list1.append((i[j]['name']))# the key 'name' contains the name of the genre
	movies.loc[index,'genres']=str(list1)
    
# 用json.loads把keywords变成字典列表后，遍历每一个keywords的长度，用name取值得到每一个列值对应的name列表
	movies['keywords']=movies['keywords'].apply(json.loads)
	for index,i in zip(movies.index,movies['keywords']):
	    list1=[]
	    for j in range(len(i)):
	        list1.append((i[j]['name']))
	movies.loc[index,'keywords']=str(list1)

# 用json.loads把production_companies变成字典列表后，遍历每一个production_companies的长度，用name取值得到每一个列值对应的name列表
	movies['production_companies']=movies['production_companies'].apply(json.loads)
	for index,i in zip(movies.index,movies['production_companies']):
	    list1=[]
	    for j in range(len(i)):
	        list1.append((i[j]['name']))
	movies.loc[index,'production_companies']=str(list1)
    
# 用json.loads把production_countries变成字典列表后，遍历每一个production_countries的长度，用name取值得到每一个列值对应的name列表
	movies['production_countries']=movies['production_countries'].apply(json.loads)
	for index,i in zip(movies.index,movies['production_countries']):
	    list1=[]
	    for j in range(len(i)):
	        list1.append((i[j]['name']))
	movies.loc[index,'production_countries']=str(list1)
    
# 用json.loads把cast变成字典列表后，遍历每一个cast的长度，用name取值得到每一个列值对应的name列表
	credits['cast']=credits['cast'].apply(json.loads)
	for index,i in zip(credits.index,credits['cast']):
	    list1=[]
	    for j in range(len(i)):
	        list1.append((i[j]['name']))
	credits.loc[index,'cast']=str(list1)

# credits['crew']里面存储了大量电影工作人员的信息，本项目仅对本项目研究中需要使用的对象进行提取用json.loads把crew变成字典列表后，遍历每一个crew值，用job得到每一个值对应的name列表
	credits['crew']=credits['crew'].apply(json.loads)
	def director(x):
	    for i in x:
	        if i['job'] == 'Director':
	            return i['name']
	credits['crew']=credits['crew'].apply(director)
	credits.rename(columns={'crew':'director'},inplace=True)

# 以上处理后，再movies和credits两张表依据电影ID关联起来
	movies=movies.merge(credits,left_on='id',right_on='movie_id',how='left')
	#用for循环语句选择单产地电影数据集，并清除产地国信息缺失的数据（无法填充）
	#由于一些电影的产地不止一个国家，是由多个国家合作制作的，为了便于统计，因此将合作的电影也清除
	movies['production_countries']=movies['production_countries'].str.strip('[]').str.replace(' ','').str.replace("'",'')
	j=0
	for i in movies['production_countries']:
	    if "," in i:
	        c="合作"
	    elif len(i)==0:
	        c="缺失"
	    else:
	        c=i.split(",")[0]
	    movies.loc[j,'production_countries']=c
	    j=j+1
	mov_country=movies['production_countries'].value_counts()
	mov_country_=mov_country.drop({"合作","缺失"})
    mov_country_data=pd.DataFrame({"country":mov_country_.index
                               ,"num":mov_country_})


@app.route('/text1',methods=['POST','GET'])
def text1() -> 'html':
	fig=px.pie(mov_country_data,values="num",names="country",title="不同国家电影产量比例",template="seaborn")
	fig.update_traces(textposition="inside",textinfo="value+percent+label")#标签位置放在里面，标签信息包含值，百分比，标签
	py.offline.plot(fig, filename="不同国家电影产量比例.html",auto_open=False)
	with open("不同国家电影产量比例.html", encoding="utf8", mode="r") as f:
	    plot_all1 = "".join(f.readlines())
	data_str = mov_country_data.to_html()
    return render_template('results6.html',
                            the_plot_all = plot,
                            the_res = mov_country_data,
                            the_select_region=regions_available,
                           )



@app.route('/text2',methods=['POST','GET'])
def text2() -> 'html':
	vote_data_=pd.DataFrame({"country":vote_data.index
                              ,"score":vote_data["mean"]})
	vote_data_s=vote_data_.sort_values(by=["score"],ascending=False)[:10]
	plt.figure(figsize=(12,8))
	ax=sns.catplot(x="country",
	            y="score",
	            data=vote_data_s,
	            kind="bar",aspect=2.5)
	plt.xlabel(xlabel="国家",fontsize=14)     # 设置x轴
	plt.ylabel(ylabel="评分",fontsize=14)     # 设置y轴
	ax.set_xticklabels(rotation=45,fontsize=14)     # 设置大小
	ax.set_yticklabels(fontsize=14)
	plt.title('平均电影评分最高的十个国家',fontsize=16,pad=20)
	py.offline.plot(plt, filename="平均电影评分最高的十个国家.html",auto_open=False)
	with open("平均电影评分最高的十个国家.html", encoding="utf8", mode="r") as f:
	    plot_all2 = "".join(f.readlines())
	data_str = vote_datas_s.to_html()
    return render_template('results6.html',
                            the_plot_all = plot,
                            the_res = vote_data_s,
                            the_select_region=regions_available,
                           )


@app.route('/text3',methods=['POST','GET'])
def text3() -> 'html':
	movies['genres']=movies['genres'].str.strip('[]').str.replace(' ','').str.replace("'",'')
	movies['genres']=movies['genres'].str.split(",")
	list_=[]
	for i in movies['genres']:
	    list_.extend(i)
	genres_data=pd.Series(list_).value_counts()[:10].sort_values(ascending=False)
	genres_data_=pd.DataFrame({"genres":genres_data.index
                               ,"num":genres_data})
	plt.figure(figsize=(12,8))
	ax=sns.catplot(x="genres",
	            y="num",
	            data=genres_data_,
	            kind="bar",aspect=2)
	plt.xlabel(xlabel="类型",fontsize=14)
	plt.ylabel(ylabel="数量",fontsize=14)
	ax.set_xticklabels(rotation=45,fontsize=14)
	ax.set_yticklabels(fontsize=14)
	plt.title('电影类型分布',fontsize=16,pad=20)
	py.offline.plot(plt, filename="电影类型分布.html",auto_open=False)
	with open("电影类型分布.html", encoding="utf8", mode="r") as f:
	    plot_all3 = "".join(f.readlines())
	data_str = geners_data_.to_html()
    return render_template('results6.html',
                            the_plot_all = plot,
                            the_res = geners_data_,
                            the_select_region=regions_available,
                           )


@app.route('/text4',methods=['POST','GET'])
def text4() -> 'html':
	genres_full_data=pd.Series(list_).value_counts().sort_values(ascending=False)
	genres_full_data_=pd.DataFrame({"genres":genres_full_data.index
	                                ,"num":genres_full_data}).sort_values(by=["genres"]).drop("")
	df=pd.DataFrame(index=range(1,2300),columns=genres_full_data_["genres"].index)
	for i in genres_full_data_["genres"]:
	    list_=[]
	    for j,t in zip(movies["genres"],movies["vote_average"]):
	        if i in j:
	            list_.append(t)
	    df[i]=pd.Series(list_)
	genre_vote_data=df.describe().T
	df

	plt.figure(figsize=(12,6))
	df_=df.T.stack().reset_index(level=1
	                            ,drop=True
	                            ,inplace= False
	                            ).reset_index().rename(columns={"index":"类型",0:"评分"})
	df_["类型"].value_counts().sort_index()
	ax=sns.boxplot(x="类型"
	               ,y="评分",
	               data=df_)
	plt.title('不同类型电影的评分情况',fontsize=16)
	plt.xlabel(xlabel="类型",fontsize=14)
	plt.ylabel(ylabel="评分",fontsize=14)
	ax.set_xticklabels(labels=list(df.columns),rotation=45,fontsize=12)
	py.offline.plot(plt, filename="不同类型电影的评分情况.html",auto_open=False)
	with open("不同类型电影的评分情况.html", encoding="utf8", mode="r") as f:
	    plot_all1 = "".join(f.readlines())
	data_str = genres_full_data_.to_html()
    return render_template('results6.html',
                            the_plot_all = plot,
                            the_res = generes_full_data_,
                            the_select_region=regions_available,
                           )


@app.route('/text5',methods=['POST','GET'])
def text5() -> 'html':
	movies['cast']=movies['cast'].str.strip('[]').str.replace(' ','').str.replace("'",'').str.replace('"','')
	movies['cast']=movies['cast'].str.split(',')
	list_=[]
	for i in movies['cast']:
	    list_.extend(i)
	cast_data=pd.Series(list_).value_counts()[:11].sort_values(ascending=False).drop("")
	cast_data_=pd.DataFrame({"cast":cast_data.index
	                               ,"num":cast_data}).drop(index="Jr.")#剔除演员姓名异常的数据
	plt.figure(figsize=(12,8))
	ax=sns.catplot(x="cast",
	            y="num",
	            data=cast_data_,
	            kind="bar",aspect=2)
	plt.title('出演频次较高的演员',fontsize=16)
	plt.xlabel(xlabel="演员",fontsize=14)
	plt.ylabel(ylabel="数量",fontsize=14)
	ax.set_xticklabels(rotation=45,fontsize=14)
	py.offline.plot(plt, filename="出演频次较高的演员.html",auto_open=False)
	with open("出演频次较高的演员.html", encoding="utf8", mode="r") as f:
	    plot_all1 = "".join(f.readlines())
	data_str = cast_data_.to_html()
    return render_template('results6.html',
                            the_plot_all = plot,
                            the_res = cast_data_,
                            the_select_region=regions_available,
                           )


@app.route('/text6',methods=['POST','GET'])
def text6() -> 'html':
	def dir(s):
	    if s is None:
	        return ''
	    return str(s)
	movies['director']=movies['director'].apply(dir)
	director_data=movies[movies['director']!=''].director.value_counts()[:10].sort_values(ascending=False)
	director_data_=pd.DataFrame({"director":director_data.index
	                               ,"num":director_data})
	plt.figure(figsize=(12,8))
	ax=sns.catplot(x="director",
	            y="num",
	            data=director_data_,
	            kind="bar",aspect=2)
	plt.title('制作电影数量较多的导演',fontsize=16)
	plt.xlabel(xlabel="导演",fontsize=14)
	plt.ylabel(ylabel="数量",fontsize=14)
	ax.set_xticklabels(rotation=45,fontsize=14)
	py.offline.plot(plt, filename="制作电影数量较多的导演.html",auto_open=False)
	with open("制作电影数量较多的导演.html", encoding="utf8", mode="r") as f:
	    plot_all1 = "".join(f.readlines())
	data_str = director_data_.to_html()
    return render_template('results6.html',
                            the_plot_all = plot,
                            the_res = director_data_,
                            the_select_region=regions_available,
                           )



if __name__ == '__main__':
    app.run(
        debug=True
    )
